﻿using System;
using System.Drawing;
using System.Runtime.Versioning;

#pragma warning disable IDE0130 // 命名空间与文件夹结构不匹配
namespace zykControl
#pragma warning restore IDE0130 // 命名空间与文件夹结构不匹配
{
    /// <summary>
    /// 本类表示zykHFGrid控件的一行,一行包含的单元格数量与列数相同
    /// </summary>
    /// <remarks></remarks>
    [SupportedOSPlatform("windows")]
    public class ZykHFGridRow
    {
        /// <summary>
        /// 对象实例化
        /// </summary>
        /// <param name="theArea">本行所在的区</param>
        /// <param name="theIndex">本行在区中的索引</param>
        /// <remarks></remarks>
        internal ZykHFGridRow(ZykHFGridArea theArea, int theIndex)
        {
            Area = theArea;
            Columns = theArea.Columns;
            Location.X = theArea.X;
            Location.Y = theArea.Y + theArea.Height;
            BackColor = theArea.BackColor;
            ForeColor = theArea.Body.ForeColor;
            Array.Resize(ref mCells, Columns.Count);
            for (int xx = 0, loopTo = Columns.Count - 1; xx <= loopTo; xx++)
                mCells[xx] = new ZykHFGridCell(this, Columns[xx]);
            Index = theIndex;
        }

        /// <summary>
        /// 对象实例化
        /// </summary>
        /// <param name="theArea">本行所在的区</param>
        /// <param name="theIndex">本行在区中的索引</param>
        /// <param name="theText">文本数组</param>
        /// <remarks></remarks>
        internal ZykHFGridRow(ZykHFGridArea theArea, int theIndex, string[] theText)
        {
            Area = theArea;
            Columns = theArea.Columns;
            Location.X = theArea.X;
            Location.Y = theArea.Y + theArea.Height;
            BackColor = theArea.BackColor;
            ForeColor = theArea.Body.ForeColor;
            Array.Resize(ref theText, Columns.Count);
            Array.Resize(ref mCells, Columns.Count);
            for (int xx = 0, loopTo = Columns.Count - 1; xx <= loopTo; xx++)
                mCells[xx] = new ZykHFGridCell(this, Columns[xx], theText[xx]);
            Index = theIndex;
        }

        public Color BackColor;
        public readonly Color ForeColor;
        public readonly Point Location = new();

        public int X => Location.X;

        public int Y => Location.Y;

        public int Width => Area.Width;

        public int Height => Area.ItemHeight;

        private readonly ZykHFGridCell[] mCells;
        public readonly int Index;

        public bool IsTop => Index == 0;

        public bool IsBottom => Index >= Count - 1;

        public int Count => mCells.Length;

        /// <summary>
        /// 检索此行包含的单元格
        /// </summary>
        /// <param name="CellIndex">单元格在此行下的索引</param>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public ZykHFGridCell this[int CellIndex] => mCells[CellIndex];

        /// <summary>
        /// 检索此行包含的单元格
        /// </summary>
        /// <param name="theColumn">单元格所在的列</param>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public ZykHFGridCell this[ZykHFGridColumn theColumn] => mCells[theColumn.Index];

        public ZykHFGridCell? SelectCell
        {
            get
            {
                int dIndex = SelectedCellIndex;
                if (dIndex == -1 || dIndex > Columns.Count - 1) return null;

                return mCells[dIndex];
            }
            set
            {
                if (value != null) SelectedCellIndex = value.Index;
            }
        }

        public readonly ZykHFGridArea Area;
        public readonly ZykHFGridColumns Columns;
        private int mSelectedCellIndex = -1;
        /// <summary>
        /// 返回/设置当前选中的单元格
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public int SelectedCellIndex
        {
            get
            {
                if (IsSelected == false)
                {
                    return -1;
                }
                else if (mSelectedCellIndex < 0)
                {
                    if (Area.Count == 1 && Count > 0)
                    {
                        return 0;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else if (mSelectedCellIndex > Count - 1)
                {
                    return Count - 1;
                }
                else
                {
                    return mSelectedCellIndex;
                }
            }

            set
            {
                if (value < -1)
                    value = -1;
                if (value > Count - 1)
                    value = Count - 1;
                bool dChange = value != SelectedCellIndex;
                if (dChange == false)
                    return;
                mSelectedCellIndex = value;
                if (value >= 0)
                {
                    if (mCells[value].IsBill == true && Index > 0)
                    {
                        Area[0].SelectedCellIndex = value;
                        CellSelectChanged?.Invoke(Area[0][value]);
                    }
                    else
                    {
                        Area.SelectedRowIndex = Index;
                        CellSelectChanged?.Invoke(mCells[value]);
                    }
                }
                else
                {
                    Area.SelectedRowIndex = -1;
                    CellSelectChanged?.Invoke(null);
                }
            }
        }

        /// <summary>
        /// 当前选中的单元格改变后发生
        /// </summary>
        /// <remarks></remarks>
        public event CellSelectChangedEventHandler? CellSelectChanged;

        public delegate void CellSelectChangedEventHandler(ZykHFGridCell? theItem);

        /// <summary>
        /// 当本行被选中时,返回True
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool IsSelected
        {
            get
            {
                if (Area.IsSelected == false)
                    return false;
                return Area.SelectedRowIndex == Index;
            }
        }

        public Color[] ItemForeColor = [];

        public void SetItemForeColor(int theIndex, Color theColor)
        {
            ItemForeColor[theIndex] = theColor;
        }

        public Color[] ItemBackColor = [];

        public void SetItemBackColor(int theIndex, Color theColor)
        {
            ItemBackColor[theIndex] = theColor;
        }

        /// <summary>
        /// 使用一个文本数组设置所有项的文本
        /// </summary>
        /// <param name="theText"></param>
        /// <remarks></remarks>
        public void SetItemText(string[] theText)
        {
            Array.Resize(ref theText, Columns.Count);
            for (int xx = 0, loopTo = Columns.Count - 1; xx <= loopTo; xx++)
            {
                if (theText[xx] is null)
                    theText[xx] = "";
                this[xx].Text = theText[xx];
            }
        }

        public void ToSelected()
        {
            Area.SelectedRowIndex = Index;
        }

        /// <summary>
        /// 返回本行从网格中所在的行数,从0开始
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public int GetBodyRowIndex()
        {
            var dCount = default(int);
            for (int xx = 0, loopTo = Area.Index - 1; xx <= loopTo; xx++)
                dCount += Area.Body[xx].Count;
            dCount += Index + 1;
            dCount -= 1;
            return dCount;
        }
    }
}